<template>
  <div class='container'>
    <div class="directorys-container">
    <el-card>
      <el-row>
        <el-col :span="18" >
          <el-form ref="subjectsForm" :model="subjectsInfo" style="display: flex;">
              <el-form-item label="学科名称"   label-width="80px">
                <el-input v-model="subjectsInfo.subjectName" @keyup.enter="handleFilter" />
              </el-form-item>
              <el-form-item>
                <el-button size="small"  style="margin-left: 10px;" @click="resetForm">清除</el-button>
                <el-button size="small" type="primary" style="margin-left: 10px;" @click="handleFilter">搜索</el-button>
              </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align: right;">
          <el-row>
            <el-col :span="24" >
              <el-button type="success" size="small" @click="AddSubjectsFn('add')">
                <i class="el-icon-edit"></i>
                <span>新增学科</span>
              </el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <!-- 警告信息 -->
      <el-alert
        :title="subjectstitle"
        type="info"
        show-icon
        :closable="false"
        style="margin-bottom: 15px;"
      />
      <el-table
      :data="dataList"
      v-loading="listLoading"
      :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}">
      <el-table-column
        type="index"
        :index="indexMethod"
        label="序号"
        width="48">
      </el-table-column>
      <el-table-column
        prop="subjectName"
        label="学科名称"
        width="208">
      </el-table-column>
      <el-table-column
        prop="username"
        label="创建者"
        width="205">
      </el-table-column>
      <el-table-column
        prop="addDate"
        label="创建日期"
        width="160">
        <template #default="{row}">
          <!-- {{row}} -->
          {{ formatTimeFn(row.addDate) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="isFrontDisplay"
        label="前台是否显示"
        width="205">
        <template #default="{row}">
          {{row.isFrontDisplay? '是' : '否'}}
        </template>
      </el-table-column>
      <el-table-column
        prop="twoLevelDirectory"
        label="二级目录"
        width="205">
      </el-table-column>
      <el-table-column
        prop="tags"
        label="标签"
        width="205">
      </el-table-column>
      <el-table-column
        prop="totals"
        label="题目数量"
        width="205">
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作"
        width="240">
        <template #default="{row}">
          <el-button
            type="text"
            size="small"
            @click="subjectsRoutes(row, 'subjects')"
          >学科分类</el-button>
          <el-button
            type="text"
            size="small"
            @click="subjectsRoutes(row, 'tage')"
          >学科标签</el-button>
          <el-button
            type="text"
            size="small"
            @click="AddSubjectsFn(row)"
          >修改</el-button>
          <el-button
            type="text"
            size="small"
            @click="delSubjects(row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <div style="height: 60px; margin-top: 10px; display:flex;justify-content:flex-end">
      <el-pagination
        small
        background
        layout="sizes, prev, pager, next, jumper"
        :total="counts"
        :current-page="requestParameters.page"
        :page-size="requestParameters.pagesize"
        :page-sizes="[5,10,15,20]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    </el-card>
    <subjects-add :formData.sync="formData" :dialogVisible.sync="dialogVisible" :dialogTitle="dialogTitle" />
    </div>
  </div>
</template>

<script>
import { list, remove, detail } from '@/api/hmmm/subjects'
import dayjs from 'dayjs'
import SubjectsAdd from '@/module-hmmm/components/subjects-add.vue'
export default {
  name: '',
  components: { SubjectsAdd },
  data () {
    return {
      /** 搜索框 */
      subjectsInfo: {
        subjectName: ''
      },
      subjectstitle: '数据一共xx条', // 轻警告提示
      requestParameters: {
        page: 1, // 当前页码
        pagesize: 10 // 单页条数
      },
      pages: 1, // 总页数
      counts: 0, // 总条数
      dataList: [], // table 展示数据
      dialogVisible: false,
      dialogTitle: '新增学科',
      listLoading: false,
      formData: {}
    }
  },
  created () {
    this.subjectsListApi()
  },
  methods: {
    /** 获取列表数据，进行页面展示 */
    async subjectsListApi () {
      this.listLoading = true
      const { data: { counts, items } } = await list({ ...this.requestParameters, ...this.subjectsInfo })
      this.counts = counts
      this.dataList = items
      this.subjectstitle = '数据一共' + counts + '条'
      this.listLoading = false
    },
    // 重置
    resetForm () {
      this.subjectsInfo.subjectName = ''
      this.subjectsListApi()
    },
    // 搜索信息
    handleFilter () {
      this.requestParameters.page = 1
      this.subjectsListApi()
    },
    /** 自定义序号 */
    indexMethod (index) {
      return (this.requestParameters.page - 1) * this.requestParameters.pagesize + index + 1
    },
    /** 入职时间格式处理 */
    formatTimeFn (data) {
      return dayjs(data).format('YYYY-MM-DD hh:mm:ss')
    },

    /** 分页---每页条目发送变化时 */
    handleSizeChange (val) {
      this.requestParameters.pagesize = val
      this.requestParameters.page = 1
      this.subjectsListApi(this.requestParameters)
    },
    /** 分页---页码发生变化时 */
    handleCurrentChange (val) {
      this.requestParameters.page = val
      this.subjectsListApi(this.requestParameters)
    },
    /** 打开添加学科 dialog */
    AddSubjectsFn (data) {
      this.dialogVisible = true
      if (data === 'add') {
        this.dialogTitle = '新增学科'
      } else {
        this.dialogTitle = '编辑学科'
        this.editSubjects(data)
      }
    },
    /** 删除学科 */
    delSubjects (data) {
      this.$confirm('你确定要删除该学科吗？', '提示')
        .then(async () => {
          await remove(data)
          this.$message.success('学科删除成功')
          this.subjectsListApi()
        })
        .catch(() => {})
    },
    /** 获取学科详情 */
    async editSubjects (data) {
      const { data: res } = await detail({ id: data.id })
      this.formData = res
    },
    /** 至目录、页签页面跳转 */
    subjectsRoutes (data, val) {
      const { id, subjectName } = data
      if (val === 'subjects') {
        this.$router.push({ path: '/subjects/directorys', query: { id, name: subjectName } })
      } else {
        this.$router.push({ path: '/subjects/tags', query: { id, name: subjectName } })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  .directorys-container{
    // padding: 10px;
    .el-card{
      .el-row:after {
        clear: both;

      }
      .el-row:before {
        display: table;
      }
      .el-form-item--medium {
    margin-bottom: 18px;
}
    }
  }
}
</style>
